

/**************************************************START OF FILE*****************************************************/




/*------------------------------------------------------------------------------------------------------------------
Includes
*/
#include "usbdeviceworker.h"
#include <QThread>
#include <QDebug>
#include "x_hiddevice.h"
#include "QEventLoop"
#include "QTimer"


/*
*********************************************************************************************************************

@ Brief  : 构造函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 15:47:51

*********************************************************************************************************************
*/

UsbDeviceWorker::UsbDeviceWorker(QObject *parent) : QObject(parent)
{

}


/*
*********************************************************************************************************************

@ Brief  : 线程启动函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 15:46:06

*********************************************************************************************************************
*/
void UsbDeviceWorker::DoWork(const QString &partmeter)
{

    Q_UNUSED(partmeter)


    HID_Device = new X_HidDevice();
    X_HidDevice::LibInit();
    QTimer* timer = new QTimer();
    connect(timer , &QTimer::timeout , this , &UsbDeviceWorker::HandleTimeOut);
    timer->start(10);
}


/*
*********************************************************************************************************************

@ Brief  : 定时器处理函数10ms

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 15:45:20

*********************************************************************************************************************
*/
void UsbDeviceWorker::HandleTimeOut(void)
{
    static uint8_t RxData[64];
    static uint32_t CheckTime = 0;

    CheckTime ++;
    if(CheckTime > 50)
    {
        CheckTime = 0;
        CheckDeviceConnect();
    }

    if(HID_Device->isDeviceOpen)
    {
        memset(RxData , 0x00 , 64);
        HID_Device->ReadData(RxData);
        if((RxData[0] == 0xAA) && (RxData[1] == 0x55))
        {
            uint8_t* arry = (uint8_t*)malloc(64);
            for(uint8_t i = 0; i < 64 ; ++i)
            {
                arry[i] = RxData[i];
            }
            emit PostMessage(arry);
        }
    }
}


/*
*********************************************************************************************************************

@ Brief  : 定时检测USB的连接断开状态

@ Param  : NONE

@ Return : NONE

@ Author : YWJ(QQ872180981)

@ Date   : 2020-08-31 15:44:21

*********************************************************************************************************************
*/
void UsbDeviceWorker::CheckDeviceConnect(void)
{
    //判断USB设备是否连接
    libusb_device **devs;
    int cnt;

    static bool LastDeviceStatus = false;

    if(HID_Device->isDeviceOpen == false)
    {
        HID_Device->OpenDevice(0x572B , 0x0483);
    }
    else
    {
        cnt = libusb_get_device_list(NULL, &devs);
        if (cnt < 0)
        {
            HID_Device->isDeviceOpen = false;
            HID_Device->Close();
        }
        else
        {
            bool isFindDevice = false;
            uint8_t index=0;
            for (; index < cnt; index++)
            {
                if(HID_Device->Device_Addr == libusb_get_device_address(devs[index]))
                {
                    isFindDevice = true;
                    break;
                }
            }
            //打不到对应的设备地址
            if(isFindDevice == false)
            {
                HID_Device->isDeviceOpen = false;
                HID_Device->Close();
            }
        }

        libusb_free_device_list(devs, 1);
    }

    uint8_t* arry = (uint8_t*)malloc(64);
    arry[0] = 0xAA;
    arry[1] = 0x55;
    arry[2] = 0xF1;

    if(HID_Device->isDeviceOpen == false)
    {
        arry[3] = 0x00;
    }
    else
    {
       arry[3] = 0x01;
    }

    if(LastDeviceStatus != HID_Device->isDeviceOpen)
    {
        emit PostMessage(arry);
    }

    LastDeviceStatus = HID_Device->isDeviceOpen;
}


/*
*********************************************************************************************************************

@ Brief  : 接收主线程的消息函数

@ Param  : NONE

@ Return : NONE

@ Author : YWJ

@ Date   : 2020-08-31 15:39:23

*********************************************************************************************************************
*/
void UsbDeviceWorker::RxMessage(uint8_t* Rxdata)
{

        HID_Device->WriteData(Rxdata);
        free(Rxdata);
}




/**************************************************END OF FILE**********************************************************/



